package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import fl.text.TLFTextField;
	
	import flash.display.*;
	import flash.display.InteractiveObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.*;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.engine.ElementFormat;
	import flash.ui.*;
	import flash.ui.Keyboard;
	import flash.utils.*;
	import flash.utils.getDefinitionByName;
	
	import flashx.textLayout.edit.EditManager;
	import flashx.textLayout.edit.SelectionManager;
	import flashx.textLayout.edit.SelectionState;
	import flashx.textLayout.elements.FlowElement;
	import flashx.textLayout.elements.ParagraphElement;
	import flashx.textLayout.elements.SpanElement;
	import flashx.textLayout.elements.TextFlow;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.plaf.*;
	import org.aswing.skinbuilder.SkinProgressBarBackground;
	import org.aswing.skinbuilder.SkinProgressBarForeground;
	import org.aswing.skinbuilder.SkinScrollBarBackground;
	import org.aswing.skinbuilder.SkinScrollBarThumb;
	import org.aswing.skinbuilder.SkinScrollBarUI;
	
	
	public class FrameChat  extends JFrame
	{
		//members define
		private var scrollpane:JScrollPane;
		private var _textArea:MultilineLabel;
		private var panelButtons:JPanel;
		private var btn_all:JToggleButton;
		private var btn_current:JToggleButton;
		private var btn_world:JToggleButton;
		private var btn_faction:JToggleButton;
		private var btn_team:JToggleButton;
		private var panel14:JPanel;
		private var btn_change_size:JButton;
		private var btn_clearup:JButton;
		private var btn_history:JButton;

		private var text:String  = new String();
		private var _stlfTextFiled:STLFTextField = null;
		private var model:BoundedRangeModel = new DefaultBoundedRangeModel();
		
		public function FrameChat()
		{
			super();
			setDragable(false);
			setClosable(false);
			setResizable(false);
			setTitleBar(null);
			setBorder(new EmptyBorder());
			//this.setBackgroundDecorator(new SolidBackground(new ASColor(0,0)));
			
			//component creation
			setSize(new IntDimension(318, 150));
			setPreferredSize(new IntDimension(318, 150));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			scrollpane = new JScrollPane();
			scrollpane.setLocation(new IntPoint(0, 18));
			scrollpane.setSize(new IntDimension(318, 132));
			scrollpane.setPreferredSize(new IntDimension(318, 132));
			
			_textArea = new MultilineLabel();
			_textArea.setSize(new IntDimension(318, 129));
			
			panelButtons = new JPanel();
			panelButtons.setLocation(new IntPoint(0, 0));
			panelButtons.setSize(new IntDimension(318, 18));
			panelButtons.setPreferredSize(new IntDimension(318, 18));
			var layout1:FlowWrapLayout = new FlowWrapLayout();
			layout1.setPreferWidth(200);
			layout1.setAlignment(AsWingConstants.LEFT);
			layout1.setHgap(0);
			layout1.setVgap(0);
			panelButtons.setLayout(layout1);
			
			btn_all = new JToggleButton();
			btn_all.setLocation(new IntPoint(9, 0));
			btn_all.setSize(new IntDimension(39, 23));
			btn_all.setPreferredSize(new IntDimension(39, 18));
			btn_all.setText("label");
			
			btn_current = new JToggleButton();
			btn_current.setLocation(new IntPoint(53, 0));
			btn_current.setSize(new IntDimension(39, 23));
			btn_current.setPreferredSize(new IntDimension(39, 18));
			btn_current.setText("label");
			
			btn_world = new JToggleButton();
			btn_world.setLocation(new IntPoint(97, 0));
			btn_world.setSize(new IntDimension(39, 23));
			btn_world.setPreferredSize(new IntDimension(39, 18));
			btn_world.setText("label");
			
			btn_faction = new JToggleButton();
			btn_faction.setLocation(new IntPoint(141, 0));
			btn_faction.setSize(new IntDimension(39, 23));
			btn_faction.setPreferredSize(new IntDimension(39, 18));
			btn_faction.setText("label");
			
			btn_team = new JToggleButton();
			btn_team.setLocation(new IntPoint(185, 0));
			btn_team.setSize(new IntDimension(39, 18));
			btn_team.setPreferredSize(new IntDimension(39, 18));
			btn_team.setText("label");
			
			panel14 = new JPanel();
			panel14.setLocation(new IntPoint(195, 0));
			panel14.setSize(new IntDimension(75, 18));
			panel14.setPreferredSize(new IntDimension(75, 18));
			
			btn_change_size = new JButton();
			btn_change_size.setLocation(new IntPoint(245, 2));
			btn_change_size.setSize(new IntDimension(16, 21));
			btn_change_size.setPreferredSize(new IntDimension(16, 18));
			btn_change_size.setToolTipText("改变尺寸");
			btn_change_size.setText("label");
			
			btn_clearup = new JButton();
			btn_clearup.setLocation(new IntPoint(248, 0));
			btn_clearup.setSize(new IntDimension(18, 18));
			btn_clearup.setPreferredSize(new IntDimension(16, 18));
			btn_clearup.setToolTipText("清除聊天信息");
			btn_clearup.setText("label");
			
			btn_history = new JButton();
			btn_history.setLocation(new IntPoint(16, 23));
			btn_history.setSize(new IntDimension(16, 18));
			btn_history.setPreferredSize(new IntDimension(16, 18));
			btn_history.setToolTipText("聊天历史记录");
			btn_history.setText("label");
			
			//component layoution
			getContentPane().append(scrollpane);
			getContentPane().append(panelButtons);
			
			scrollpane.append(_textArea);
			
			panelButtons.append(btn_all);
			panelButtons.append(btn_current);
			panelButtons.append(btn_world);
			panelButtons.append(btn_faction);
			panelButtons.append(btn_team);
			panelButtons.append(panel14);
			panelButtons.append(btn_change_size);
			panelButtons.append(btn_clearup);
			panelButtons.append(btn_history);
			
			
			//setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_chat_bg") as DisplayObject));
			//some text for demo
			//_output.html = true;
			//var text:String  = new String();
			//text +='<style>a:link { font-size: 14pt;  color: #000000; } a:hover { font-size: 14pt; color: #ff6600; }a:visited { font-size: 14pt; color: #cccccc; }</style>'

			//text += '<font color="#FFFF00"></font>欢迎进入游戏!<br>';
			//text +='<font color="#FFFF00">『人人西游』: 如果女人是<font color="#ef7dcf">玫瑰花</font>,那么男人就是<font color="#00FF00">玫瑰花的叶子!</font></font><br>';
			
			//text +='<font color="#FFFF00">『小小西游』: 珍惜手中的机会!</font><br>';
			//addSystemMessage("珍惜手中的机会!");
			/*text += '(2011-09-14,白色已完成)<br>';
			text += '<font color="#FFFF00">2011-08-22:</font>技术准备,策划案<br>';
			text += '<font color="#FFFF00">2011-08-24:</font>服务器框架搭建<br>';
			text += '<font color="#FFFF00">2011-09-04:</font>地图编辑器<br>';
			text += '<font color="#FFFF00">2011-09-04:</font>通信协议,角色数据缓存<br>';
			text += '<font color="#FFFF00">2011-09-11:</font>登陆创建角色流程<br>';
			text += '<font color="#FFFF00">2011-09-11:</font>地图功能实现(移动,切换地图,相互可视)<br>';
			text += '<font color="#FFFF00">2011-09-11:</font>主界面,选择线路<br>';
			text += '<font color="#FFFF00">2011-09-18:</font>人物属性<br>';
			text += '<font color="#FFFF00">2011-09-24:</font>男女角色原画的最终定稿(art)<br>';
			text += '<font color="#FFFF00">2011-09-25:</font>物品,装备穿戴<br>';
			text += '<font color="#FFFF00">2011-09-25:</font>技能初步<br>';
			text += '<font color="#FFFF00">2011-09-27:</font>战斗UI(art)<br>';
			text += '<font color="#FFFF00">2011-09-29:</font>任务UI和查看玩家信息UI(art)<br>';
			text += '<font color="#FFFF00">2011-10-01:</font>改资源组织方式,重写客户端<br>';
			text += '<font color="#FFFF00">2011-10-01:</font>男女各自一套动作(art)<br>';
			text += '<font color="#FFFF00">2011-10-07:</font>技能特效四组(art)<br>';
			text += '<font color="#FFFF00">2011-10-07:</font>男女装备各一(art)<br>';			
			text += '<font color="#FFFF00">2011-10-07:</font>战斗(缺脚本和特效)<br>';
			text += '<font color="#FFFF00">2011-10-07:</font>宠物<br>';
			text += '<font color="#FFFF00">2011-10-14:任务(延后)</font><br>';
			text += '<font color="#FFFF00">2011-10-21:</font>队伍<br>';
			text += '<font color="#FFFF00">2011-10-21:</font>地图两张(art)<br>';
			text += '<font color="#FFFF00">2011-10-21:</font>好友,聊天<br>';
			text += '<font color="#FFFF00">2011-10-28:</font>排行榜<br>';
			text += '<font color="#FFFF00">2011-10-28:</font>商城<br>';
			text += '<font color="#FFFF00">2011-10-31:</font>交易<br>';
			text += '<font color="#FFFF00">2011-11-05:</font>世界地图(简易)<br>';
			text += '<font color="#FFFF00">2011-11-05:男女装备各一(art)</font><br>';
			text += '<font color="#FFFF00">2011-11-05:地图三张(art)</font><br>';
			text += '<font color="#FFFF00">... ...</font><br>';
			//text += '<font color="#FFFF00">暂用测试地址:</font><a href="http://119.145.144.150"><u><i><font color="#0000FF">点此处进入</font></i></u></a>');
			text += '<font color="#FFFF00">暂用测试地址:</font><a href="http://119.145.144.150"><u><font color="#0000FF">点此处进入</font></u></a><br>';*/

			//text += '', [ { index:0, src:_smileys[4] }, { index:0, src:_smileys[4] }, { index:0, src:_smileys[4] }, { index:0, src:_smileys[4] } ] );
			//text += '<font color="#00FFFF">啦啦啦游戏作品</font><br>';
			
			//text += '<a href=\"event:物品1\">展示这个,看下:<font color="#0000FF">物品1</font></a><br>';
			//text += '<a href=\"event:物品2\">还要展示这个:<font color="#0000FF">物品2</font></a><br>';
			//_output.addEventListener(TextEvent.LINK, linkHandler);
			
			/*_inputScrollBar = getUIScrollBar();
			_inputScrollBar.scrollTarget = _output.textfield;
			_inputScrollBar.x = _output.x + _output.width;
			_inputScrollBar.y = _output.y;
			_inputScrollBar.height = _output.height;
			addChild(_inputScrollBar);*/

			
			//_textArea = new JTextArea();
			//_textArea.setLocationXY(10,8);	
			_textArea.setFont(new ASFont("宋体", 13, false, false, false, false));
			_textArea.setForeground(new ASColor(0x85edb8));
			_textArea.setEditable(false);

			_textArea.setForeground(new ASColor(0xffffff, 1));
			_textArea.setBackground(new ASColor(0x033332, 1));
			_textArea.setEditable(false);
			_textArea.setWordWrap(true);
			_textArea.setBorder( new EmptyBorder);
			_textArea.setBackgroundDecorator(null);
			
			_textArea.setHtmlText(text);
			_textArea.setWordWrap(true);
			_textArea.scrollToBottomLeft();

			_textArea.addEventListener(TextEvent.LINK, linkHandler);

			addSystemMessage("珍惜手中的机会!");
			Client.getInstance().addEventListener("1000", phChatMessage_1000);
			Client.getInstance().addEventListener("1002", phChatMessageBattler_1002);
			
			var filter:GlowFilter = new GlowFilter(0x000000, 1, 2, 2, 5);
			_textArea.filters = new Array(filter);
			getContentPane().addEventListener(MouseEvent.ROLL_OVER, onRollOver);
			getContentPane().addEventListener(MouseEvent.ROLL_OUT, onRollOut);
			//getContentPane().addEventListener(MouseEvent.CLICK, onClick);
			_textArea.addEventListener(MouseEvent.CLICK, onClick);

			panelButtons.setVisible(false);
			//scrollbar.setVisible(false);
			//scrollbar.setModel(model);
			model.addStateListener(_stateChanged);
			setBackgroundDecorator(null);
			//scrollbar.setForeground(new ASColor(0x033332,1.0));
			//scrollbar.setBackground(new ASColor(0x033332,1.0));
			//scrollbar.set
			scrollpane.getVerticalScrollBar().setVisible(true);
			scrollpane.setVerticalScrollBarPolicy(JScrollPane.SCROLLBAR_ALWAYS);
			scrollpane.setHorizontalScrollBarPolicy(JScrollPane.SCROLLBAR_NEVER);
			scrollpane.setLayout(new ScrollPaneLayout(ScrollPaneLayout.TOP_LEFT));
			this.setClipMasked(false);
			this.getContentPane().setClipMasked(false);
			setImages();
		}

		private function _stateChanged(e:Event):void
		{
			
		}
		//private var _filterArray:Array = new Array(new GlowFilter(0xffff00, 1, 20, 20));
		private var background:AssetBackground = null;//new SolidBackground(new ASColor(0x033332,0.8));
		private function onRollOver(event:MouseEvent):void
		{
			//event.target.filters = _filterArray;
			//setBackgroundDecorator(background);
			if(background == null)
			{
				var myMatrix:Matrix = new Matrix();
				myMatrix.createGradientBox(width, height, 1.6, 50, 50);
				var myColors:Array = [0x033332, 0x033344];
				var myAlphaS:Array = [100, 100];
				var myRalphaS:Array = [0, 225];
				var square:Sprite = new Sprite();
				square.graphics.beginFill(0x033332, 0.8);
				square.graphics.drawRoundRectComplex(0,0,width,height,5,5,5,5);
				square.graphics.endFill();
				background = new AssetBackground(square as DisplayObject);
			}
			setBackgroundDecorator(background);
			panelButtons.setVisible(true);
			//scrollpane.getVerticalScrollBar().setVisible(true);
			//scrollpane.setVerticalScrollBarPolicy(JScrollPane.SCROLLBAR_ALWAYS);
			this.paintImmediately();
			//scrollbar.setVisible(true);
		}
		
		private function onRollOut(event:MouseEvent):void
		{
			if(currentHeightMultiple != 0)
			{
				setBackgroundDecorator(null);
				panelButtons.setVisible(false);
			}
			//scrollpane.getVerticalScrollBar().setVisible(false);
			//scrollpane.setVerticalScrollBarPolicy(JScrollPane.SCROLLBAR_NEVER);
			this.paintImmediately();
			//scrollbar.setVisible(false);
			//event.target.filters = null;
		}
		private var clickLink:Boolean = false;
		private function onClick(event:MouseEvent):void
		{
			if(event.target is JButton) return;
			if(event.target is JToggleButton) return;
			if(event.target is JScrollBar) return;
			if(clickLink)
			{
				clickLink = false;
				return;
			}
			//if(event.target is TextField) return;
			//trace("event.target:"+event.target+",event.currentTarget:"+event.currentTarget);
			if(!SceneMain.getInstance().onClick(event))
				PanelMain.getInstance().onClick(event);
		}
		
		//private var _scrollPane:JScrollPane = null
		public function setImages():void
		{
			//setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_chat_bg") as DisplayObject));
			//this.setBackground(new ASColor(0xeeeeee,0.8));
			//setBackgroundDecorator(new SolidBackground(new ASColor(0x111111,0.8)));
			setBackgroundDecorator(null);
			btn_clearup.wrapSimpleButton(Client.getUIElement("btn_clearup") as SimpleButton);
			btn_clearup.setText("");
			
			btn_change_size.wrapSimpleButton(Client.getUIElement("btn_change_size") as SimpleButton);
			btn_change_size.setText("");
			btn_history.wrapSimpleButton(Client.getUIElement("btn_history") as SimpleButton);
			btn_history.setText("");
			
			btn_all.wrapSimpleButton(Client.getUIElement("btn_chat_all") as SimpleButton);
			btn_all.setText("");
			btn_current.wrapSimpleButton(Client.getUIElement("btn_chat_current") as SimpleButton);
			btn_current.setText("");
			btn_faction.wrapSimpleButton(Client.getUIElement("btn_chat_faction") as SimpleButton);
			btn_faction.setText("");
			btn_team.wrapSimpleButton(Client.getUIElement("btn_chat_team") as SimpleButton);
			btn_team.setText("");
			btn_world.wrapSimpleButton(Client.getUIElement("btn_chat_world") as SimpleButton);
			btn_world.setText("");
			
			//btn_all.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			//btn_all.setText("全部");
			/*btn_current.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_current.setText("当前");
			btn_faction.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_faction.setText("帮派");
			btn_team.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_team.setText("队伍");
			btn_world.wrapSimpleButton(Client.getUIElement("btn_2") as SimpleButton);
			btn_world.setText("世界");*/
			
			btn_all.setHorizontalTextPosition(AsWingConstants.CENTER);
			btn_all.setVerticalTextPosition(AsWingConstants.CENTER);
			btn_current.setHorizontalTextPosition(AsWingConstants.CENTER);
			btn_current.setVerticalTextPosition(AsWingConstants.CENTER);
			btn_faction.setHorizontalTextPosition(AsWingConstants.CENTER);
			btn_faction.setVerticalTextPosition(AsWingConstants.CENTER);
			btn_team.setHorizontalTextPosition(AsWingConstants.CENTER);
			btn_team.setVerticalTextPosition(AsWingConstants.CENTER);
			btn_world.setHorizontalTextPosition(AsWingConstants.CENTER);
			btn_world.setVerticalTextPosition(AsWingConstants.CENTER);
			
			btn_all.setSelected(true);
			var group:ButtonGroup = new ButtonGroup();
			group.appendAll(btn_all,btn_current,btn_faction,btn_team,btn_world);
			//group.setAllowUncheck(true);
			
			btn_all.addActionListener(onBtnSelectChannel);
			btn_current.addActionListener(onBtnSelectChannel);
			btn_world.addActionListener(onBtnSelectChannel);
			btn_faction.addActionListener(onBtnSelectChannel);
			btn_team.addActionListener(onBtnSelectChannel);
			
			btn_clearup.addActionListener(onBtnClearup);
			btn_change_size.addActionListener(onBtnChangeSize);
			btn_history.addActionListener(onBtnHistory);
			
			scrollpane.getVerticalScrollBar().setUIElement(true);
			scrollpane.getVerticalScrollBar().setUI(new MyScrollBarUI);
			
			baseHeight = scrollpane.height;
			
			
		}
		protected function onBtnSelectChannel(e:Event):void
		{
			update();
		}
		protected function onBtnClearup(e:Event):void
		{
			_messages.splice(0,_messages.length);
			update();
		}
		
		private var currentHeightMultiple:Number = 1;
		private var baseHeight:int = 0;
		protected function onBtnChangeSize(e:Event):void
		{
			currentHeightMultiple +=0.5;
			if(currentHeightMultiple > 2.5)
			{
				setBackgroundDecorator(background);
				panelButtons.setVisible(true);
				currentHeightMultiple = 0.5;
			}
			
			setSize(new IntDimension(width, baseHeight*currentHeightMultiple+panelButtons.height));
			setPreferredSize(new IntDimension(width, baseHeight*currentHeightMultiple+panelButtons.height));
			panelButtons.setLocation(new IntPoint(0, 0));
			
			scrollpane.setLocation(new IntPoint(0, 18));
			scrollpane.setSize(new IntDimension(width, baseHeight*currentHeightMultiple));
			scrollpane.setPreferredSize(new IntDimension(width, baseHeight*currentHeightMultiple));
			
			_frame.y = (Engine.H - this.height - 28);
			
			background = null;
			if(getBackgroundDecorator() != null)
			{
				var myMatrix:Matrix = new Matrix();
				myMatrix.createGradientBox(width, height, 1.6, 50, 50);
				var myColors:Array = [0x033332, 0x033344];
				var myAlphaS:Array = [100, 100];
				var myRalphaS:Array = [0, 225];
				var square:Sprite = new Sprite();
				square.graphics.beginFill(0x033332, 0.8);
				square.graphics.drawRoundRectComplex(0,0,width,height,5,5,5,5);
				square.graphics.endFill();
				background = new AssetBackground(square as DisplayObject);
				setBackgroundDecorator(background);
			}
			
			pack();
			this.paintImmediately();
		}
		protected function onBtnHistory(e:Event):void
		{
			
		}
		private static var _instance:* = null;
		public static function getInstance():FrameChat
		{
			if(_instance == null)
				_instance = new FrameChat();
			return _instance;
		}
		private function linkHandler(linkEvent:TextEvent):void 
		{
			clickLink = true;
			//trace("linkEvent.target:"+linkEvent.target+",linkEvent.currentTarget:"+linkEvent.currentTarget);
			var dtemp:Array = (linkEvent.text as String).split(',');
			if(dtemp.length != 5)
			{
				return;
			}
			
			var obj:Object = new Object;
			obj.m_nCharId = int(dtemp[0]);
			obj.m_szCharName = dtemp[1];
			obj.m_nLevel = int(dtemp[2]);
			obj.m_nRace = int(dtemp[3]);
			obj.m_nGender = int(dtemp[4]);
			
			//trace(linkEvent.text);
			//FrameInfo.showMessageDialog("","这是一个物品"+linkEvent.text);
			var point:IntPoint = AsWingUtils.getStageMousePosition();
			FrameClickChar._objDest = obj;
			FrameClickChar._destCharId = int(obj.m_nCharId);
			FrameClickChar.show(_frame,point.x-_frame.x,point.y-_frame.y);
			//FrameClickChar.show(0,0);
			//trace("point.x:"+point.x+" point.y:"+point.y);
		}

		
		public var textMessage:String  = new String();
		protected function phChatMessage_1000(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phChatMessage_1000", msg);
			var nIndex:uint = 0;
			var map_id:int = 0;//msg[0];
			var line_id:int = 0;//msg[0];
			var m_nCharId:int = 0;//msg[0];
			var battler_id:int = 0;
			var m_szCharName:String = "";
			var m_nLevel:int = 0;
			var m_nRace:int = 0;
			var m_nGender:int = 0;
			var chat_msg:String = "";
			var type:int = msg[nIndex++];	
			
			var message:String = new String;
			if(type == 0)
			{
				chat_msg = msg[nIndex++];
				addSystemMessage(chat_msg);
			}
			else
			{
				map_id = msg[nIndex++];
				line_id = msg[nIndex++];
				m_nCharId = msg[nIndex++];
				battler_id = msg[nIndex++];
				m_szCharName = msg[nIndex++];
				m_nLevel = msg[nIndex++];
				m_nRace = msg[nIndex++];
				m_nGender = msg[nIndex++];
				chat_msg = msg[nIndex++];
				addChatMessage(type,map_id,line_id,m_nCharId,m_szCharName,m_nLevel,m_nRace,m_nGender,chat_msg);
			}
			
			
			if(type != 0)
			{
				var char:ObjectChar = null;
				if(PanelMain.getInstance().isInBattle)
				{
					char = SceneBattle.getInstance().getObjectByBattlerId(battler_id);
					if(char == null)
						return;
					//else
					//	char = SceneMain.getInstance().getObjectByCharId(m_nCharId);
				}
				else
					char = SceneMain.getInstance().getObjectByCharId(m_nCharId);
				if(char != null)
				{
					//char.addChatBubble(chat_msg);
					var panel:PanelChatBubble = new PanelChatBubble;
					panel.setMessage(chat_msg);
					char.addPanelChatBubble(panel);
				}
			}
		}
		/*private function linkHandlerChat(linkEvent:TextEvent):void 
		{
			//var charId:int = int(linkEvent.text);
			
			var m_nCharId:int = 0;
			var m_szCharName:String = "";
			var m_nLevel:int = 0;
			var m_nRace:int = 0;
			var m_nGender:int = 0;
			
			var dtemp:Array = (linkEvent.text as String).split(',');
			if(dtemp.length != 5)
			{
				return;
			}
			m_nCharId = dtemp[0];
			m_szCharName = dtemp[0];
			m_nLevel = dtemp[0];
			m_nRace = dtemp[0];
			m_nGender = dtemp[0];
			//Client.getInstance().sendPacketLogic(1302,[charId]);
		}*/
		protected function phChatMessageBattler_1002(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phChatMessageMonster_1002", msg);
			var battler_id:int = msg[0];
			var message:String = msg[1];
			var char:ObjectChar = SceneBattle.getInstance().getObjectByBattlerId(battler_id);
			
			if(char != null)
			{
				//char.addChatBubble(message);
				var panel:PanelChatBubble = new PanelChatBubble;
				panel.setMessage(message);
				char.addPanelChatBubble(panel);
			}
		}
		private var maxLength:int = 1024*16;
		private var _messages:Array = new Array;
		/*public function addMessage(msg:String):void
		{
			text += msg+"<br>";
			if(text.length > maxLength)
			{
				text = text.slice(text.length - maxLength,text.length);
				var lastBR:int = text.indexOf("<br>");
				text = text.slice(lastBR,text.length);
			}
			_textArea.setHtmlText(text);
			_textArea.scrollToBottomLeft();
		}*/
		public function addSystemMessage(chat_msg:String):void
		{
			var message:String = new String;
			message += '<font color="#85edb8">『小小西游』: '+chat_msg+"</font>";
			var obj:Object = new Object;
			obj.message = message;
			obj.type = 0;
			_messages.push(obj);
			update();
		}
		public function addChatMessage(type:int,map_id:int,line_id:int,m_nCharId:int,m_szCharName:String,m_nLevel:int,m_nRace:int,m_nGender:int,msg:String):void
		{
			var objMapCSV:Object = Client.getInstance().GetMapCSV(map_id);
			//labelDesc.setText('等级:'+_item.require_level+(objItemCSV == null?"":" " + objItemCSV.name));
			var message:String = new String;
			if(objMapCSV == null)
				return;
				
			
			if(type == 1)message += '『当前』';
			if(type == 2)message += '『世界』';
			if(type == 3)message += '『帮派』';
			if(type == 4)message += '『队伍』';

			if(m_nGender == 0)
				message += '<font color="#6fb5eb">';
			if(m_nGender == 1)
				message += '<font color="#e81c73">';
			
			if(m_nGender == 0)
			{
				message += '♂';
			}
			if(m_nGender == 1)
			{
				message += '♀';
			}
			message += '<a href=\"event:'+m_nCharId+','+m_szCharName+','+m_nLevel+','+m_nRace+','+m_nGender+'\"><u>';
			message +=  m_szCharName + '</u></a>: </font>'
			/*if(m_nGender == 0)
			{
				message += '<font color="#6fb5eb">' + msg+"</font>";
			}
			if(m_nGender == 1)
			{
				message += '<font color="#e81c73">' + msg+"</font>";
			}*/

			message += msg;
			message += '<font color="#999999"> [' + objMapCSV.name+'('+line_id+")]</font>";
			var obj:Object = new Object;
			obj.message = message;
			obj.type = type;
			_messages.push(obj);
			update();
			
		}
		public function update():void
		{
			var showType:int = 0;//all
			if(btn_all.isSelected()) showType = 0;
			if(btn_current.isSelected()) showType = 1;
			if(btn_world.isSelected()) showType = 2;
			if(btn_faction.isSelected()) showType = 3;
			if(btn_team.isSelected()) showType = 4;
			
			text = "";
			if(_messages.length > 512)
			{
				_messages.splice(0,_messages.length - 512);
			}
			for each(var obj:Object in _messages)
			{
				if(showType == 0 || showType == obj.type)
				{
					text += obj.message+"<br>";
					//addMessage(obj.message);
				}
			}
			_textArea.setHtmlText(text);
			_textArea.scrollToBottomLeft();
		}
		
		private static var _frame:FrameChat = null;
		public static function show():void
		{
			if(_frame == null)
			{
				_frame = FrameChat.getInstance();
				PanelMainHelp.getInstance().addChild(_frame);
			}
			_frame.show();
			PanelMainHelp.getInstance().bringToTop(_frame);
			_frame.x = 4;
			//_frame.y = 423;
			_frame.y = (Engine.H - _frame.height - 28);
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
	}
}